﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PRJ_LDW_Eutoliso.AccountAdmin.Endereco.Estado
{
    public partial class Edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            //string idEstado = Request.QueryString["Id"].Trim();
            
            if (!IsPostBack)

            {               

                try
                {

                    string idEstado = Request.QueryString["Id"].Trim();

                    eutolisodbEntities1 context = new eutolisodbEntities1();

                    int id_Estado = Convert.ToInt32(idEstado);

                    var _query = (from est in context.estado
                                  where est.Id == id_Estado select new
                                      { 
                                          est.Id,
                                          est.Nome,
                                          est.Regiao,
                                          est.Uf
                                      });


                    foreach (var Result in _query)
                    {
                        txtNome.Text = Result.Nome;
                        ddlRegiao.SelectedValue = Result.Regiao;
                        txtUF.Text = Result.Uf;

                        
                        
                    }


                }
                catch (Exception)
                {
                    
                   
                }

            
            }


        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/AccountAdmin/Endereco/Estado/Home.aspx");
            
        }

        protected void btnInserir_Click(object sender, EventArgs e)
        {
            try
            {

                string idEstado = Request.QueryString["Id"].Trim();
                eutolisodbEntities1 context = new eutolisodbEntities1();

                    int id_Estado = Convert.ToInt32(idEstado);

                    var _query = (from est in context.estado
                                  where est.Id == id_Estado 
                                  select est).First();



                    _query.Nome = txtNome.Text;
                    _query.Regiao = ddlRegiao.SelectedValue;
                    _query.Uf = txtUF.Text;

                    context.SaveChanges();
                    
                }    

      
            catch (Exception)
            {


            }

            Response.Redirect("/AccountAdmin/Endereco/Estado/Home.aspx");
        }

       
    }
}